package org.example.what_mall.handler;

import lombok.extern.slf4j.Slf4j;
import org.example.what_mall.exception.GlobalCode;
import org.example.what_mall.exception.GlobalException;
import org.example.what_mall.result.HttpResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(GlobalException.class)
    public HttpResult<String> handleGlobalException(GlobalException e) {
        log.error("定义全局异常: {}", e.getMessage());
        return HttpResult.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public HttpResult<String> handleException(Exception e) {
        log.error("非定义全局异常: {}", e.getMessage());
        return HttpResult.error(GlobalCode.SERVER_ERROR.getCode(), e.getMessage());
    }
}
